1 package edu.jiangxin.apktoolbox.convert.color.colorspace; 2 3 import java.awt.*; 4 import java.awt.color.ColorSpace; 5 import java.io.Serial; 6 7 public class HsbColorSpace extends ColorSpace { 8 @Serial 9 private static final long serialVersionUID = 1L; 10 11 protected HsbColorSpace(int type, int numComponents) { 12 super(type, numComponents); 13 } 14 15 public static HsbColorSpace getInstance() { 16 return Holder.INSTANCE; 17 } 18 19 @Override 20 public float[] toRGB(float[] colorvalue) { 21 int rgb = Color.HSBtoRGB(colorvalue[0], colorvalue[1], colorvalue[2]); 22 return new float[]{ 23 (rgb >> 16 & 0xFF) / 255f, 24 (rgb >> 8 & 0xFF) / 255f, 25 (rgb & 0xFF) / 255f 26 }; 27 } 28 29 @Override 30 public float[] fromRGB(float[] rgbvalue) { 31 int r = (int) (rgbvalue[0] * 255); 32 int g = (int) (rgbvalue[1] * 255); 33 int b = (int) (rgbvalue[2] * 255); 34 return Color.RGBtoHSB(r, g, b, null); 35 } 36 37 @Override 38 public float[] toCIEXYZ(float[] colorvalue) { 39 float[] rgb = toRGB(colorvalue); 40 return ColorSpace.getInstance(CS_sRGB).toCIEXYZ(rgb); 41 } 42 43 @Override 44 public float[] fromCIEXYZ(float[] colorvalue) { 45 float[] rgb = ColorSpace.getInstance(CS_sRGB).fromCIEXYZ(colorvalue); 46 return fromRGB(rgb); 47 } 48 49 private static class Holder { 50 private static final HsbColorSpace INSTANCE = new HsbColorSpace(TYPE_HSV, 3); 51 } 52 }